{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bff59bf0-f102-4b2e-a61a-351403c69b94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找到目标设备，正在打开: http://192.168.101.3:8080\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import re\n",
    "import subprocess\n",
    "import webbrowser\n",
    "\n",
    "def find_target_ip(target_mac):\n",
    "    # 获取ARP表（根据系统选择命令）\n",
    "    if os.name == 'nt':  # Windows\n",
    "        arp_output = subprocess.check_output(['arp', '-a'], text=True)\n",
    "    else:  # Linux/macOS\n",
    "        arp_output = subprocess.check_output(['arp', '-a'], text=True)\n",
    "    \n",
    "    # 匹配IP和MAC地址\n",
    "    pattern = re.compile(r'(\\d+\\.\\d+\\.\\d+\\.\\d+).*({})'.format(target_mac.replace(':', r'[:-]')))\n",
    "    match = pattern.search(arp_output)\n",
    "    return match.group(1) if match else None\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    target_mac = \"d0:49:7c:0c:58:bd\"  # 修改为目标设备的MAC地址，注意是小写\n",
    "    target_port = 8080  # 目标端口\n",
    "    \n",
    "    target_ip = find_target_ip(target_mac)\n",
    "    if target_ip:\n",
    "        url = f\"http://{target_ip}:{target_port}\"\n",
    "        print(f\"找到目标设备，正在打开: {url}\")\n",
    "        webbrowser.open(url)\n",
    "    else:\n",
    "        print(\"未找到目标设备，请确认设备在线且MAC地址正确。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "68b3c401-7e9e-4b12-bb12-49c7c4f86a37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'_gateway (192.168.101.1) 位于 30:3f:7b:7b:bc:04 [ether] 在 wlo1\\n'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subprocess.check_output(['arp', '-a'], text=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85e60fc3-b725-44f2-8e76-f287e8fcab63",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba5cc458-dac8-47a8-b3d9-98029f91da8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
